package com.base.service;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

import com.base.util.DateConverter;
import com.config.Config;
import com.lazyzhao.string.StringUtils;
import com.lazyzhao.util.collection.ListUtil;
import com.lazyzhao.util.date.DateConvertUtils;


public class FilesService extends BasicService{
	
	
	public List<Map<String,Object>> findFiles(String folder) throws SQLException{
		List<String> folderlist = new ArrayList<String>();
		List<String> filelist = new ArrayList<String>();
		
		File folderFile = getRealFile(folder);
		Map<String,Map<String,Object>> fileInfoMap = new HashMap<String, Map<String,Object>>();
		List<Map<String,Object>> fileInfoList = new ArrayList<Map<String,Object>>();
		
		if(folderFile == null || !folderFile.exists()){
			return fileInfoList;
		}
		
		Map<String,Object> info = null;
		String fname = "";
		int flag = 0;
		for(File f : folderFile.listFiles()){
			info = new HashMap<String, Object>();
			fname = f.getName();
			flag = fname.lastIndexOf(".");
			if(f.isDirectory()){
				folderlist.add(fname);
				info.put("isDirectory", "true");
			}else{
				filelist.add(fname);
				info.put("ext", flag==-1?"":fname.substring(flag+1));
				info.put("isDirectory", "false");
			}
			info.put("id", fname);
			info.put("name", fname);
			info.put("size", (int)Math.ceil(f.length()/1024f));
			info.put("upatetime", DateConvertUtils.dateToSimpleDateTimeString(new Date(f.lastModified())));
			fileInfoMap.put(fname, info);
		}
		
		folderlist = ListUtil.listOrderByDefaultAsc(folderlist);
		filelist = ListUtil.listOrderByDefaultAsc(filelist);
		
		for(String f : folderlist){
			fileInfoList.add(fileInfoMap.get(f));
		}
		for(String f : filelist){
			fileInfoList.add(fileInfoMap.get(f));
		}
		return fileInfoList;
	}
	
	public void deleteFiles(String[] files){
		for(String file : files){
			FileUtils.deleteQuietly(getRealFile(file));
		}
	}
	
	public boolean mkdirs(String folder){
		return getRealFile(folder).mkdirs();
	}
	
	public void deleteFile(String filestr) throws IOException{
			File file = getRealFile(filestr);
			if(file != null){
				if(file.isDirectory()){
					FileUtils.deleteDirectory(file);
				}else{
					file.delete();
				}
			}
	}
	
	public File getRealFile(String file){
		String root = Config.getProperty("files_root_path");
		if(root.startsWith("file://")){
			root = root.substring(7);
		}else{
			root = Config.realPath + root;
		}
		root = root.replaceAll("\\\\|/", StringUtils.noRegexSpecialChar(File.separator));
		if(!root.endsWith(File.separator)){
			root += File.separator;
		}
		root += file;
		root = root.replaceAll("\\\\|/", StringUtils.noRegexSpecialChar(File.separator));
		return new File(root);
	}
	
	public static void main(String[] args) {
		//System.out.println("c://sdfs:/fff\\dsf\\".replaceAll("\\\\|/", StringUtils.noRegexSpecialChar(File.separator)));
		try {
			System.out.println(com.lazyzhao.io.file.FileUtils.fileToBase64("c:/Users/zk/Desktop/20120211_200.txt").replaceAll("\r\n", " "));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
